package org.fnzn.web.fngl;

import org.apache.log4j.Logger;
import org.fnzn.aop.SystemControllerLog;
import org.fnzn.bean.FnglProject;
import org.fnzn.bean.FnglProjectType;
import org.fnzn.bean.vo.User;
import org.fnzn.core.Page;
import org.fnzn.core.Result;
import org.fnzn.service.FnglProjectService;
import org.fnzn.util.CommonUtil;
import org.fnzn.util.ContextUtil;
import org.fnzn.util.FileUpload;
import org.fnzn.web.SysLogController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目信息管理
 */
@RestController
@RequestMapping(value = "/fnglProject")
@EnableAutoConfiguration
public class FnglProjectController {
    Logger log = Logger.getLogger(SysLogController.class);

    @Value("${upload.path}")
    private String path;

    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private FnglProjectService fnglProjectService;

    @RequestMapping(value = "/list",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description="查询项目")
    public Result findFnglProject(FnglProject fnglProject, Page page){
        return fnglProjectService.findFnglProject(fnglProject,page);
    }

    @RequestMapping(value = "/add",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    @SystemControllerLog(description="新增项目")
    public Result insertFnglProject(FnglProject fnglProject){
        return  fnglProjectService.insertFnglProject(fnglProject);
    }

    @RequestMapping(value = "/update",produces = "application/json;charset=UTF-8",method = RequestMethod.PUT)
    @SystemControllerLog(description="修改项目")
    public Result updateFnglProject(FnglProject fnglProject){
        return  fnglProjectService.updateFnglProject(fnglProject);
    }

    @RequestMapping(value = "/{projectId}",produces = "application/json;charset=UTF-8",method = RequestMethod.DELETE)
    @SystemControllerLog(description="删除项目")
    public Result deleteFnglProject(@PathVariable String projectId){
        return  fnglProjectService.deleteFnglProject(projectId);
    }

    @RequestMapping(value = "/deleteAll",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description="删除多个项目")
    public Result deleteAllFnglProject(String projectIds){
        return fnglProjectService.deleteAllFnglProject(projectIds);
    }

    @RequestMapping(value = "/getProjectList",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description="根据项目类型获取到对应的项目列表")
    public List<FnglProject> getProjectList(String projectType){
        return fnglProjectService.getProjectList(projectType);
    }

    @RequestMapping(value = "/getProjectType",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description="获取到项目类型列表")
    public List<FnglProjectType> getProjectType(){
        return fnglProjectService.getProjectType();
    }

    //处理文件上传
    @RequestMapping(value = "/uploadimg", method = RequestMethod.POST)
    public Map<String,String> uploadImg(@RequestParam("file") MultipartFile file,
                                  HttpServletRequest request) {

        //图片文件类型
        String contentType = file.getContentType();
        //图片名字
        String fileName = file.getOriginalFilename();
        //文件存放路径
//        String filePath = "C:\\Users\\lvqi\\Desktop\\";
        String filePath = "/home/fn_estate/picture/";
        //调用文件处理类FileUtil，处理文件，将文件写入指定位置
        try {
            FileUpload.uploadFile(file.getBytes(), filePath, fileName);

        } catch (Exception e) {
            // TODO: handle exception
        }
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
//        String[] array = new String[1];
//        array[0] = basePath+"/fnglProject/getimg/"+fileName;
        Map<String,String> map = new HashMap<>();
        map.put("imgUrl",basePath+"/fnglProject/getimg/"+fileName);
        // 返回图片的存放路径
        return map;
    }

    @RequestMapping(value = "/getimg/**", method = RequestMethod.GET)
    public ResponseEntity getImg(HttpServletRequest request) {

        String url = request.getRequestURI().replace("fnglProject/getimg","");
        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_JPEG)
                .body(resourceLoader.getResource("file:" + path + url));
    }

    /**
     *  根据房源id获取到房源图片
     */
    @RequestMapping(value = "/getProjectImg", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @SystemControllerLog(description = "获取项目图片")
    public String[] getProjectImg(String projectId) {
        return fnglProjectService.getProjectImg(projectId);
    }

    /**
     *  根据当前登录用户所属项目的项目类型获取新房项目或者二手房项目
     */
    @RequestMapping(value = "/getProject", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @SystemControllerLog(description = "获取项目id和名称")
    public Result getFnglProject(){

        User user = CommonUtil.getUser();
        return fnglProjectService.getFnglProject(user);
    }

    /**
     *  录入房源时获取到所有的二手房项目
     */
    @RequestMapping(value = "/getHouseProject",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description = "获取二手房项目")
    public Result getHouseProject(){
        return fnglProjectService.getHouseProject();
    }


}
